//
//  AYNavigationController.swift
//  MyWeibo
//
//  Created by 昌俊 杨 on 29/9/2016.
//  Copyright © 2016 April. All rights reserved.
//

import UIKit

class AYNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationBar.isHidden = true
    }
    
    
    /// 重写 push 方法，所有的 push 动作都会调用此方法！
    /// viewController 是被 push 的控制器，设置他的左侧的按钮作为返回按钮

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
          // 如果不是栈底控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count>0
        {
            viewController.hidesBottomBarWhenPushed = true
           /** 在第一次push的之前只有一个控制器 */
            let title:String = childViewControllers.count == 1 ? childViewControllers[0].title! : "返回"
             /** 给自定的navibar添加pop按钮及事件 */
            let vc = viewController as? AYBaseViewController
            vc?.naviItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popBack), isback: true)
        }
        
        
        super.pushViewController(viewController, animated: animated)
    }
    func popBack() {
        popViewController(animated: true)
    }
    
}
